import { defineConfig } from 'vite'

// 自动导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'

// Element plus 和 SVG
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// Naive UI
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'path'
const pathSrc = path.resolve(__dirname, "src");

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: '0.0.0.0',
    port: 8080,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost/', // 修改为你的后端 API 地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''), // 去掉请求路径中的 '/api' 前缀
      },
    },
  },
  plugins: [
    vue(),
    vueJsx(),
    // 自动导入element plus和vue
    AutoImport({
      imports: ["vue", "@vueuse/core", "vue-router", {
        'naive-ui': [
          'useDialog',
          'useMessage',
          'useNotification',
          'useLoadingBar',
          'NIcon'
        ]
      }],
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver(), NaiveUiResolver()],
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]',
      // 自定义插入的位置
      inject: "body-last",
      customDomId: "__svg__icons__dom__"
    }),
  ],
  resolve: {
    alias: {
      '@': pathSrc
    }
  }
})
